package com.wujie.market.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wujie.market.dao.GoodsDao;
import com.wujie.market.dao.OrderdetailDao;
import com.wujie.market.po.Goods;
import com.wujie.market.po.Orderdetail;
import com.wujie.market.service.OrderdetailService;
import org.apache.ibatis.jdbc.Null;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Orderdetail)表服务实现类
 *
 * @author 
 * @since 2023-01-27 19:47:57
 */
@Service("orderdetailService")
public class OrderdetailServiceImpl implements OrderdetailService {
    @Resource
    private OrderdetailDao orderdetailDao;
    @Resource
    private GoodsDao goodsDao;

    @Override
    public List<Orderdetail> listAll() {
        return orderdetailDao.selectList(null);
    }


    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void addOrder(List<Orderdetail> orderdetailList) {
        UpdateWrapper<Goods> updatewrapper = new UpdateWrapper();
        QueryWrapper<Goods> queryWrapper=new QueryWrapper<>();
        Goods goods = new Goods();
        for (Orderdetail orderdetail:orderdetailList
             ) {
            orderdetailDao.insert(orderdetail);//插入订单表

            //将货物表的库存减少
            queryWrapper.eq("g_id",orderdetail.getgId());
            goods = goodsDao.selectOne(queryWrapper);
            goods.setCurNum(goods.getCurNum()-orderdetail.getSaleNum());
            goods.setSaleNum(goods.getSaleNum()+orderdetail.getSaleNum());
            updatewrapper.eq("g_id",goods.getgId());

            goodsDao.update(goods,queryWrapper);
        }
    }

    @Override
    public void insertOne(Orderdetail orderdetail) {
        orderdetailDao.insert(orderdetail);
    }
}
